package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.bean.PersonalShopBean;
import com.lc.luzhengtang.conn.ConnPersonalShopType;
import com.lc.luzhengtang.fragment.SimpleShoppingFragment;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.zcx.helper.activity.AppV4Activity;
import com.zcx.helper.http.AsyCallBack;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class PersonalShoppingActivity extends AppV4Activity implements OnTabSelectListener {

    @BindView(R.id.tl_2)
    SlidingTabLayout tl2;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private final String[] mTitles = {
            "胶囊", "清热解毒", "药片"

    };
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.img_share)
    ImageView imgShare;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;

    private List<PersonalShopBean.DataBean> personalshopBean;

    private UMShareListener mShareListener;
    private ShareAction mShareAction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_shopping);
        ButterKnife.bind(this);
        if (TextUtils.isEmpty(BaseApplication.basePreferences.readUserName())) {
            textTitle.setText(BaseApplication.basePreferences.readMobile() + "的店");
        } else {
            textTitle.setText(BaseApplication.basePreferences.readUserName() + "的店");
        }

        personalshopBean = new ArrayList<>();
        viewPager.setOffscreenPageLimit(5);

        getPersonalShopType();

        mShareListener = new PersonalShoppingActivity.CustomShareListener(this);
        /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(PersonalShoppingActivity.this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE
        )

                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        if (snsPlatform.mShowWord.equals("umeng_sharebutton_copy")) {
                            Toast.makeText(PersonalShoppingActivity.this, "复制文本按钮", Toast.LENGTH_LONG).show();
                        } else if (snsPlatform.mShowWord.equals("umeng_sharebutton_copyurl")) {
                            Toast.makeText(PersonalShoppingActivity.this, "复制链接按钮", Toast.LENGTH_LONG).show();

                        }else if(share_media == SHARE_MEDIA.SMS) {
                            new ShareAction(PersonalShoppingActivity.this).withText("来自分享面板标题")
                                    .setPlatform(share_media)
                                    .setCallback(mShareListener)
                                    .share();
                        }else
                        {
                            UMWeb web = new UMWeb("http://luzhengtang.cn/index.php/mobile/user/shop?member_id="+BaseApplication.basePreferences.readUid());
                            web.setTitle("清君堂");
                            web.setDescription("我的店铺");
//                            web.setThumb(new UMImage(PersonalShoppingActivity.this, R.mipmap.logo));
                            new ShareAction(PersonalShoppingActivity.this).withMedia(web)
                                    .setPlatform(share_media)
                                    .setCallback(mShareListener)
                                    .share();
                        }
                    }
                });
    }


    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @OnClick({R.id.img_news, R.id.img_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.img_share:
                mShareAction.open();
                break;
        }
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return personalshopBean.size();
            // return mTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
//
            // return mTitles[position];
            return personalshopBean.get(position).getTitle();

        }

        @Override
        public Fragment getItem(int position) {
            //  return mFragments.get(position);
            return mFragments.get(position);
        }
    }


    public void getPersonalShopType() {
        new ConnPersonalShopType(new AsyCallBack<PersonalShopBean>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }


            @Override
            public void onSuccess(String toast, int type, Object o, PersonalShopBean personalShopBean) throws Exception {
                super.onSuccess(toast, type, o, personalShopBean);
                //  Toast.makeText(PersonalShoppingActivity.this, "成功", Toast.LENGTH_SHORT).show();
                /*for (String title : mTitles) {
                    mFragments.add(SimpleShoppingFragment.getInstance(title));
                }*/
                personalshopBean = personalShopBean.getData();
                for (int i = 0; i < personalshopBean.size(); i++) {
                    mFragments.add(SimpleShoppingFragment.getInstance(personalShopBean.getData().get(i).getId() + ""));
                }
                mAdapter = new MyPagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(mAdapter);
                tl2.setViewPager(viewPager);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(PersonalShoppingActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(PersonalShoppingActivity.this, false);
    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<PersonalShoppingActivity> mActivity;

        private CustomShareListener(PersonalShoppingActivity activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(mActivity.get(), platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL
                        && platform != SHARE_MEDIA.FLICKR
                        && platform != SHARE_MEDIA.FOURSQUARE
                        && platform != SHARE_MEDIA.TUMBLR
                        && platform != SHARE_MEDIA.POCKET
                        && platform != SHARE_MEDIA.PINTEREST

                        && platform != SHARE_MEDIA.INSTAGRAM
                        && platform != SHARE_MEDIA.GOOGLEPLUS
                        && platform != SHARE_MEDIA.YNOTE
                        && platform != SHARE_MEDIA.EVERNOTE) {
                    Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                if (t != null) {
                    Log.d("throw", "throw:" + t.getMessage());
                }
            }

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

            Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }

}
